﻿using System;
using System.Data;
using System.Windows.Forms;
using UniMana.Data;
using UniMana.Utils.Data;
using UniMana.Utils.Helper;
namespace UniMana.Win.Leadership
{
    public partial class FrmReport_SubStudentSelect : DevExpress.XtraEditors.XtraForm
    {
        //private bds m_bds;
        private const int ACTION_NO = 0;

        private DBMain m_DBMain = new DBMain();
        DBMain m_data = new DBMain();
        private DBMain m_ReportStudentSelect = new DBMain();
        int m_iDepartmentKey = 0;
        int m_iMajorKey = 0;
        int m_iTrainingTypeKey = 0;
        int m_iCourseKey = 0;
        int m_iLevelKey = 0;
        DataTable dt = new DataTable();
        private int _Flag = 1;

        public FrmReport_SubStudentSelect()
        {
            InitializeComponent();
            LoadData();
            InitControlEvent();
        }

        public void InitControlEvent()
        {
            this.xucBar1.btnPrint_Click += 
                new UniMana.Utils.Controls.xucBar.ButtonClickHandler(this.btnPrint_Click);
           
           
        }

        /// <summary>
        /// Load dữ liệu lên lookupedit
        /// </summary>
        public void LoadData()
        {
            try
            {
                dt = this.m_ReportStudentSelect.m_DCReadOnly.Catalogue_Courses.ToDataTable();
                this.lueCatalogue_Course.GetData(dt, "Catalogue_CourseKey", "Name", noChoose: true);
                this.lueCatalogue_Course.EditValue = -1;

               
                dt = 
                    (this.m_DBMain.m_DCReadOnly.Catalogue_Departments.GetDepartmentInTrainingProgram(
                    this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                    this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                lueCatalogue_Department.GetData(dt, noChoose: true);

                dt = 
                    (this.m_DBMain.m_DCReadOnly.Catalogue_Majors.GetMajorInTraingProgram(
                    this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                    m_DBMain.m_DCReadOnly.Catalogue_Levels)).ToDataTable();
                lueCatalogue_Major.GetData(dt, noChoose: true);

                dt = 
                    (this.m_DBMain.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeInTraingProgram(
                    this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                    this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                lueCatalogue_TrainingType.GetData(dt, noChoose: true);
                lueCatalogue_TrainingType.EditValue = 2;

                dt = 
                    (this.m_DBMain.m_DCReadOnly.Catalogue_Courses.GetCourseKeyNameInTrainingProgram(
                    this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                    this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                lueCatalogue_Course.GetData(dt, noChoose: true);

                dt = 
                    (this.m_DBMain.m_DCReadOnly.Catalogue_Levels.GetLevelInTrainingProgram(
                    this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                    this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                lueCatalogue_Level.GetData(dt, noChoose: true);


            }
            catch { };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>


        private int CheckInfo()
        {
            if ((lueCatalogue_Department.EditValue != null) && 
                (lueCatalogue_Major.EditValue != null) && 
                (lueCatalogue_TrainingType.EditValue != null) && 
                (lueCatalogue_Course.EditValue != null) && 
                (lueCatalogue_Level.EditValue != null))
            {
                m_iDepartmentKey = (int)lueCatalogue_Department.EditValue;
                m_iMajorKey = (int)lueCatalogue_Major.EditValue;
                m_iTrainingTypeKey = (int)lueCatalogue_TrainingType.EditValue;
                m_iCourseKey = (int)lueCatalogue_Course.EditValue;
                m_iLevelKey = (int)lueCatalogue_Level.EditValue;
                return 1;
            }
            else
            {
                return 0;
            }
        }
   

  /// <summary>
  ///  view dữ liệu
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
        private void btnView_Click(object sender, EventArgs e)
        {
            if (CheckInfo() == 1)
            {
                try
                {
                    
                    //them so thu tu vao luoi
                    DataTable dt = 
                        m_ReportStudentSelect.m_DCReadOnly.Training_Student_Classes.GetReportStudentSelectData(
                        m_ReportStudentSelect.m_DCReadOnly.Catalogue_Courses, 
                        m_ReportStudentSelect.m_DCReadOnly.Catalogue_Departments, 
                        m_ReportStudentSelect.m_DCReadOnly.Catalogue_Levels, 
                        m_ReportStudentSelect.m_DCReadOnly.Catalogue_Majors, 
                        m_ReportStudentSelect.m_DCReadOnly.Catalogue_TrainingTypes, 
                        this.lueCatalogue_Course.GetIntValue(), 
                        this.lueCatalogue_Level.GetIntValue(), 
                        this.lueCatalogue_Department.GetIntValue(), 
                        this.lueCatalogue_Major.GetIntValue(), 
                        this.lueCatalogue_TrainingType.GetIntValue()).ToDataTable();
                    dt.Columns.Add("STT");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dt.Rows[i]["STT"] = i + 1;
                    }
                    this.grcStudentSelect.DataSource = dt;
                    cacStudentSelect.DataSource = dt;

                    cacStudentSelect.Series[0].ArgumentDataMember = "TenKhoaHoc";
                    cacStudentSelect.Series[0].TopNOptions.Enabled = true;
                    cacStudentSelect.Series[0].Name = "Số sinh viên của ngành";
                    cacStudentSelect.Series[0].TopNOptions.Enabled = true;
                    cacStudentSelect.Series[0].ValueDataMembersSerializable = "TongSoSV";
                }
                catch { MessageBox.Show("Lỗi", "Thông báo"); }
            }
            else
            {
                this.grcStudentSelect.DataSource = null;
                MessageBox.Show("Chưa chọn đầy đủ thông tin", "Thông báo");
            }
        }
        
        /// <summary>
        /// sự kiện lookup edit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        
  

        private void btnPrint_Click(object sender, EventArgs e)
        {

            compositeLinkHienThi.ShowPreview();
        }

        private void lueCatalogue_Major_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Major.EditValue) > 0)
                {
                    //if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue) <= 0)
                    //{
                    dt = 
                        (this.m_DBMain.m_DCReadOnly.Catalogue_Majors.GetDepartmentKeyByMajor(
                        this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                        GetValueLookUpEdit.ConvertValueToInt(
                        lueCatalogue_Major.EditValue))).ToDataTable();
                    _Flag = 0;
                    lueCatalogue_Department.EditValue = dt.Rows[0][0];
                    _Flag = 1;
                    //}

                    //if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue) > 0)
                    //{
                    dt = 
                        (this.m_DBMain.m_DCReadOnly.Catalogue_Majors.GetLevelKeyByDepartmentMajor(
                        this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                        GetValueLookUpEdit.ConvertValueToInt(
                        lueCatalogue_Department.EditValue), 
                        GetValueLookUpEdit.ConvertValueToInt(
                        lueCatalogue_Major.EditValue))).ToDataTable();
                    lueCatalogue_Level.EditValue = dt.Rows[0][0];
                    //}
                    //else
                    //{
                    //    // no action
                    //}
                }
                else
                {
                    // no action
                }
            }
            catch { }
        }

        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (_Flag == 1)
                {
                    if (GetValueLookUpEdit.ConvertValueToInt(
                        lueCatalogue_Department.EditValue) <= 0)
                    {
                        // Nạp lại dữ liệu cho bậc đào tạo, ngành, loại hình đào tạo, khóa học

                        lueCatalogue_Level.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_Levels.GetLevelInTrainingProgram(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                        lueCatalogue_Level.GetData(dt, noChoose: true);

                        lueCatalogue_Major.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_Majors.GetMajorInTraingProgram(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            m_data.m_DCReadOnly.Catalogue_Levels)).ToDataTable();
                        lueCatalogue_Major.GetData(dt, noChoose: true);

                        lueCatalogue_TrainingType.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeInTraingProgram(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                        lueCatalogue_TrainingType.GetData(dt, noChoose: true);


                        //lueCatalogue_Course.EditValue = null;
                        //dt = (this.m_DBMain.m_DCReadOnly.Catalogue_Courses.GetCourseInTrainingProgram(this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                        //lueCatalogue_Course.GetData(dt, noChoose: true);
                    }
                    else
                    {
                        // Lấy dữ liệu cho bậc đào tạo, ngành, loại hình đào tạo, khóa học theo khoa

                        lueCatalogue_Level.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_Levels.GetLevelByDepartment(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            this.m_DBMain.m_DCReadOnly.Catalogue_Majors, 
                            GetValueLookUpEdit.ConvertValueToInt(
                            lueCatalogue_Department.EditValue))).ToDataTable();
                        this.lueCatalogue_Level.GetData(dt, noChoose: true);

                        lueCatalogue_Major.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_Majors.GetMajorByDepartment(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            m_data.m_DCReadOnly.Catalogue_Levels, 
                            GetValueLookUpEdit.ConvertValueToInt(
                            lueCatalogue_Department.EditValue))).ToDataTable();
                        this.lueCatalogue_Major.GetData(dt, noChoose: true);

                        lueCatalogue_TrainingType.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeByDepartment(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            this.m_DBMain.m_DCReadOnly.Catalogue_Majors, 
                            GetValueLookUpEdit.ConvertValueToInt(
                            lueCatalogue_Department.EditValue))).ToDataTable();
                        this.lueCatalogue_TrainingType.GetData(dt, noChoose: true);

                        lueCatalogue_Course.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_Courses.GetCourseByDepartment(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            this.m_DBMain.m_DCReadOnly.Catalogue_Majors, 
                            GetValueLookUpEdit.ConvertValueToInt(
                            lueCatalogue_Department.EditValue))).ToDataTable();
                        this.lueCatalogue_Course.GetData(dt, noChoose: true);
                    }
                }
            }
            catch { }
        }

        private void lueCatalogue_Level_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Level.EditValue) > 0)
                {
                    if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue) > 0)
                    {
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_Majors.GetMajorKeyByDepartmentLevel(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms,
                            GetValueLookUpEdit.ConvertValueToInt(
                            lueCatalogue_Department.EditValue), 
                            GetValueLookUpEdit.ConvertValueToInt(
                            lueCatalogue_Level.EditValue))).ToDataTable();
                        lueCatalogue_Major.EditValue = dt.Rows[0][0];
                    }
                    else
                    {
                        // no action
                    }
                }
                else
                {
                    // no action
                }
            }
            catch { }
        }
    }
}

